package homework;

import java.util.Arrays;

public class Homework8_23 {
    public static void main(String[] args) {

        int[] arr = {1,2,3};
        System.out.println(Arrays.toString(transform(arr)));
        System.out.println(isContinuousOddNumber(arr));
        System.out.println(theMost(arr));
        System.out.println(appearOnce(arr));
        System.out.println(Arrays.toString(find(arr,9)));
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
        adjust(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println(dichotomy(arr,7));
    }

    //1.奇数位于偶数之前
    public static void adjust(int[] arr){
        int i = 0;
        int j = arr.length - 1;
        while(i < j){
            while(i < j && arr[i] % 2 != 0){
                i++;
            }
            while(i < j && arr[j] % 2 == 0){
                j--;
            }
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }

    //2.二分法
    public static int dichotomy(int[] arr,int toFind){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = left + ((right - left) >> 1);
            if(toFind > arr[mid]){
                left = mid + 1;
            } else if (toFind < arr[mid]) {
                right = mid - 1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    //3.冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]){
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }

    //4.两数之和
    public static int[] find(int[] nums,int target){
        int[] ret = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                    return ret;
                }
            }
        }
        return ret;
    }

    //5.只出现一次的数字
    public static int appearOnce(int[] arr){
        int ret = 0;
        for(int i : arr){
            ret ^= i;
        }
        return ret;
    }

    //6.多数元素
    public static int theMost(int[] arr) {
        int major = 0;
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (count == 0) {
                major = arr[i];
            }
            if (arr[i] == major) {
                count++;
            } else {
                count--;
            }
        }
        return major;
    }

    //7.存在连续三个奇数的数组
    public static boolean isContinuousOddNumber(int[] arr){
        for (int i = 0; i < arr.length - 2; i++) {
            if(arr[i] % 2 == 1 && arr[i + 1] % 2 == 1 && arr[i + 2] % 2 == 1) {
                return true;
            }
        }
        return false;
    }
    //8.改变原有数组元素的值
    public static int[] transform(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
        return arr;
    }
}
